package grpc

import (
	_ "embed"
	filev1 "gitee.com/xiao_hange/go-admin-api/api/proto/gen/file/v1"
	_ "gitee.com/xiao_hange/go-admin-pkg/pkg/grpcx/balancer/swrr"
	"gitee.com/xiao_hange/go-admin-pkg/pkg/grpcx/resolver/consul"
	consulClient "github.com/hashicorp/consul/api"
	"github.com/spf13/viper"
)

//go:embed failover.json
var svcCfg string

// failover 需要重试错误
// "UNAVAILABLE",
// "UNKNOWN",
// "DEADLINE_EXCEEDED"

func InitFileGrpcClientForConsul(client *consulClient.Client) filev1.FileServiceClient {
	var cfg config
	err := viper.UnmarshalKey("grpc.client.file", &cfg)
	cfg.ServiceConfig = svcCfg
	if err != nil {
		panic(err)
	}

	cd, err := consul.NewBuilder(client, &consul.ConsulResolverConfig{
		CacheFile: "tmp/cache/consul",
	})

	if err != nil {
		panic(err)
	}
	cc, err := InitGrpcClient(cd, "consul", "file-grpc-server", cfg)
	if err != nil {
		panic(err)
	}
	return filev1.NewFileServiceClient(cc)
}

//func InitFileGrpcClinetForErcd(client *clientv3.Client) filev1.FileServiceClient {
//
//	var cfg config
//	err := viper.UnmarshalKey("grpc.client.file", &cfg)
//	if err != nil {
//		panic(err)
//	}
//
//	bd, err := etcdResolver.NewBuilder(client)
//
//	if err != nil {
//		panic(err)
//	}
//	cc, err := InitGrpcClient(bd, "etcd", "file-grpc-server", cfg)
//	if err != nil {
//		panic(err)
//	}
//	return filev1.NewFileServiceClient(cc)
//}
